﻿/// <reference path="_namespace.js" />
/// <reference path="../References.js" />

ajax.lang.addNamespace("ajax.xna");
$imports("ajax.math.Matrix");
$imports("ajax.math.Vector3");
$imports("ajax.drawing.Point");

ajax.xna.GameGrid = function(game, dimensions, cellSize)
{///<param name="game" type="ajax.xna.Game"/>
 ///<param name="dimensions" type="ajax.drawing.Point"/>
 ///<param name="cellSize" type="ajax.drawing.Point"/>
    ajax.xna.GameGrid.superConstructor.call(this, game);
    this.preInitialize(dimensions, cellSize);    
}
ajax.lang.extend("ajax.xna.GameGrid", "ajax.xna.GameComponent");
if ($getDesignMode())
    ajax.xna.GameGrid.prototype = ajax.xna.GameComponent.prototype;


// Public members
ajax.xna.GameGrid.prototype.referenceMatrix = ajax.math.Matrix.prototype;
ajax.xna.GameGrid.prototype.dimensions      = ajax.drawing.Point.prototype;
ajax.xna.GameGrid.prototype.size            = ajax.drawing.Point.prototype;
ajax.xna.GameGrid.prototype.halfSize        = ajax.drawing.Point.prototype;
ajax.xna.GameGrid.prototype.cellSize        = ajax.drawing.Point.prototype;
ajax.xna.GameGrid.prototype._maxPosition    = ajax.drawing.Point.prototype;
ajax.xna.GameGrid.prototype._minPosition    = ajax.drawing.Point.prototype;

// Public Methods
ajax.xna.GameGrid.cast = function(obj)
{///<returns type="ajax.xna.GameGrid"/>
    return obj;
}

ajax.xna.GameGrid.prototype.preInitialize = function(dimensions, cellSize)
{
    var Matrix              = ajax.math.Matrix;
    // Hold onto what we were passed.
    this.referenceMatrix    = Matrix.identity();
    this.cellSize           = ajax.drawing.Point.cast(cellSize);
    this.dimensions         = ajax.drawing.Point.cast(dimensions);         
}

ajax.xna.GameGrid.prototype.initialize = function()
{
    this.size               = new ajax.drawing.Point(this.dimensions.x * this.cellSize.x, this.dimensions.y * this.cellSize.y);
    this.halfSize           = new ajax.drawing.Point(this.size.x / 2, this.size.y / 2); 
}

ajax.xna.GameGrid.prototype.update = function(gameTime)
{///<param name="gameTime" type="ajax.drawing.Timer"/>
    
    // Update the min/max from the reference matrix.
    var refPos = this.referenceMatrix.getPositionVector();
    this._maxPosition.x = refPos.x + this.halfSize.x;
    this._maxPosition.y = refPos.z + this.halfSize.y;
    this._minPosition.x = refPos.x - this.halfSize.x;
    this._minPosition.y = refPos.z - this.halfSize.y;   
}

ajax.xna.GameGrid.prototype.wrapVector = function(positionVector3)
{///<param name="vec" type="ajax.math.Vector3"/>
 ///<summary>Pass in a position vector and this method will adjust the values to wrap 
 /// it around the reference matrix position. 
 /// Please note that the vector Z will be used against the grid Y.</summary>
    
    // Wrap around the viewer (reference Matrix). 
    while (positionVector3.x > this._maxPosition.x)
        positionVector3.x -= this.size.x;
    while (positionVector3.x < this._minPosition.x)
        positionVector3.x += this.size.x;
    // Use Z POS instead of Y
    while (positionVector3.z > this._maxPosition.y)
        positionVector3.z -= this.size.y;
    while (positionVector3.z < this._minPosition.y)
        positionVector3.z += this.size.y;               
}